package soccer.view;

import javax.swing.DefaultListModel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JLabel;
import javax.swing.ListModel;
import javax.swing.SwingConstants;
import javax.swing.border.TitledBorder;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.image.SampleModel;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import javax.swing.UIManager;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.AbstractListModel;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;
import javax.swing.ListSelectionModel;
import javax.swing.border.CompoundBorder;
import javax.swing.event.ListDataListener;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import soccer.model.player.Player;
import javax.swing.JButton;

public class MatchDetailPanel extends JPanel {
	private JPanel awayTeamPanel;
	private JPanel homeTeamPanel;
	private JLabel homeTeamLabel;
	private JLabel awayTeamLabel;
	private JLabel lblNewLabel;
	private JLabel label;
	private JScrollPane homeTeamFirst11ScrollPane;
	private JList homeTeamFrist11ScrollList;
	private JScrollPane awayTeamFirst11ScrollPane;
	private JList awayTeamFirst11ScrolList;
	private JLabel lblGoals;
	private JScrollPane homeTeamGoalsScrollPane;
	private JLabel aLabel;
	private JScrollPane awayTeamGoalScrollPane;
	private JTable homeTeamGoalsTable;
	private JTable awayTeamGoalsTable;
	private JButton btnBack;
	

	/**
	 * Create the panel.
	 */
	public MatchDetailPanel() {
		setLayout(null);
		
		homeTeamPanel = new JPanel();
		homeTeamPanel.setBorder(new TitledBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(102, 0, 0)), "Home Team", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(102, 0, 0)));
		homeTeamPanel.setBounds(28, 28, 197, 298);
		add(homeTeamPanel);
		homeTeamPanel.setLayout(null);
		
		homeTeamLabel = new JLabel("");
		homeTeamLabel.setBounds(25, 21, 150, 14);
		homeTeamLabel.setHorizontalAlignment(SwingConstants.CENTER);
		homeTeamPanel.add(homeTeamLabel);
		
		lblNewLabel = new JLabel("First 11 Players");
		lblNewLabel.setBounds(10, 46, 129, 14);
		homeTeamPanel.add(lblNewLabel);
		
		homeTeamFirst11ScrollPane = new JScrollPane();
		homeTeamFirst11ScrollPane.setBounds(10, 61, 98, 118);
		homeTeamPanel.add(homeTeamFirst11ScrollPane);
		
		homeTeamFrist11ScrollList = new JList();
		homeTeamFirst11ScrollPane.setViewportView(homeTeamFrist11ScrollList);
		homeTeamFirst11ScrollPane.setColumnHeaderView(new JLabel("Players"));
		
		lblGoals = new JLabel("Goals :");
		lblGoals.setBounds(10, 190, 46, 14);
		homeTeamPanel.add(lblGoals);
		
		homeTeamGoalsScrollPane = new JScrollPane();
		homeTeamGoalsScrollPane.setBounds(10, 209, 177, 78);
		homeTeamPanel.add(homeTeamGoalsScrollPane);
		
		homeTeamGoalsTable = new JTable();
		homeTeamGoalsTable.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"Goal Minute", "Player"
			}
		));
		homeTeamGoalsTable.getColumnModel().getColumn(1).setPreferredWidth(109);
		homeTeamGoalsScrollPane.setViewportView(homeTeamGoalsTable);
	
		
		awayTeamPanel = new JPanel();
		awayTeamPanel.setBorder(new TitledBorder(null, "Away Team", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(102, 0, 0)));
		awayTeamPanel.setBounds(235, 30, 197, 296);
		add(awayTeamPanel);
		awayTeamPanel.setLayout(null);
		
		awayTeamLabel = new JLabel("");
		awayTeamLabel.setBounds(31, 22, 156, 14);
		awayTeamPanel.add(awayTeamLabel);
		
		label = new JLabel("First 11 Players");
		label.setBounds(10, 43, 129, 14);
		awayTeamPanel.add(label);
		
		awayTeamFirst11ScrollPane = new JScrollPane();
		awayTeamFirst11ScrollPane.setBounds(10, 57, 98, 117);
		awayTeamPanel.add(awayTeamFirst11ScrollPane);
		awayTeamFirst11ScrollPane.setColumnHeaderView(new JLabel("Players"));
		
		awayTeamFirst11ScrolList = new JList();
		awayTeamFirst11ScrollPane.setViewportView(awayTeamFirst11ScrolList);
		
		aLabel = new JLabel("Goals :");
		aLabel.setBounds(10, 189, 46, 14);
		awayTeamPanel.add(aLabel);
		
		awayTeamGoalScrollPane = new JScrollPane();
		awayTeamGoalScrollPane.setBounds(10, 205, 177, 80);
		awayTeamPanel.add(awayTeamGoalScrollPane);
		
		awayTeamGoalsTable = new JTable();
		awayTeamGoalsTable.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"Goal Minute", "Player"
			}
		));
		awayTeamGoalsTable.getColumnModel().getColumn(1).setPreferredWidth(116);
		awayTeamGoalScrollPane.setViewportView(awayTeamGoalsTable);
		
		btnBack = new JButton("Back");
		btnBack.setBounds(185, 327, 89, 23);
		add(btnBack);

	}
	 public void updateHomeTeamPlayerList(List<Player> players , JList list){
	      DefaultListModel listModel = new DefaultListModel();
	   
	    for(int i = 0; i < players.size(); i++){
	        Player p = players.get(i);
	    	String name = p.getName() + p.getSurname();
	    	listModel.addElement(name);
	    }
	    list.setModel(listModel);
	 }
	 public void updateGoalsTable(List goals, JTable table){
		 DefaultTableModel defaultTableModel = (DefaultTableModel) table.getModel();
		 resetTeamTable(defaultTableModel);
		 if(goals == null || goals.size() < 1){
			 return;
		 }
		 for(int i = 0; i < goals.size(); i++){
		   Map goalMap  =  (Map)goals.get(i);
		   Vector<Comparable> v = new Vector<Comparable>();
           v.add((Comparable)goalMap.get("golMinute"));
           v.add((Comparable)(goalMap.get("name").toString() + goalMap.get("surname").toString()));
           defaultTableModel.addRow(v);
		 }
	 }
	 public void resetTeamTable(DefaultTableModel model){
			while(model.getRowCount() > 0){
				model.removeRow(0);
			}
		}
	public void addBtnBackActionListener(ActionListener l){
		 btnBack.addActionListener(l);
	}
	public JPanel getAwayTeamPanel() {
		return awayTeamPanel;
	}
	public void setAwayTeamPanel(JPanel awayTeamPanel) {
		this.awayTeamPanel = awayTeamPanel;
	}
	public JPanel getHomeTeamPanel() {
		return homeTeamPanel;
	}
	public void setHomeTeamPanel(JPanel homeTeamPanel) {
		this.homeTeamPanel = homeTeamPanel;
	}
	public JLabel getHomeTeamLabel() {
		return homeTeamLabel;
	}
	public void setHomeTeamLabel(JLabel homeTeamLabel) {
		this.homeTeamLabel = homeTeamLabel;
	}
	public JLabel getAwayTeamLabel() {
		return awayTeamLabel;
	}
	public void setAwayTeamLabel(JLabel awayTeamLabel) {
		this.awayTeamLabel = awayTeamLabel;
	}
	public JLabel getLblNewLabel() {
		return lblNewLabel;
	}
	public void setLblNewLabel(JLabel lblNewLabel) {
		this.lblNewLabel = lblNewLabel;
	}
	public JLabel getLabel() {
		return label;
	}
	public void setLabel(JLabel label) {
		this.label = label;
	}
	public JScrollPane getHomeTeamFirst11ScrollPane() {
		return homeTeamFirst11ScrollPane;
	}
	public void setHomeTeamFirst11ScrollPane(JScrollPane homeTeamFirst11ScrollPane) {
		this.homeTeamFirst11ScrollPane = homeTeamFirst11ScrollPane;
	}
	public JList getHomeTeamFrist11ScrollList() {
		return homeTeamFrist11ScrollList;
	}
	public void setHomeTeamFrist11ScrollList(JList homeTeamFrist11ScrollList) {
		this.homeTeamFrist11ScrollList = homeTeamFrist11ScrollList;
	}
	public JScrollPane getAwayTeamFirst11ScrollPane() {
		return awayTeamFirst11ScrollPane;
	}
	public void setAwayTeamFirst11ScrollPane(JScrollPane awayTeamFirst11ScrollPane) {
		this.awayTeamFirst11ScrollPane = awayTeamFirst11ScrollPane;
	}
	public JList getAwayTeamFirst11ScrolList() {
		return awayTeamFirst11ScrolList;
	}
	public void setAwayTeamFirst11ScrolList(JList awayTeamFirst11ScrolList) {
		this.awayTeamFirst11ScrolList = awayTeamFirst11ScrolList;
	}
	public JLabel getLblGoals() {
		return lblGoals;
	}
	public void setLblGoals(JLabel lblGoals) {
		this.lblGoals = lblGoals;
	}
	public JScrollPane getHomeTeamGoalsScrollPane() {
		return homeTeamGoalsScrollPane;
	}
	public void setHomeTeamGoalsScrollPane(JScrollPane homeTeamGoalsScrollPane) {
		this.homeTeamGoalsScrollPane = homeTeamGoalsScrollPane;
	}
	public JScrollPane getAwayTeamGoalScrollPane() {
		return awayTeamGoalScrollPane;
	}
	public void setAwayTeamGoalScrollPane(JScrollPane awayTeamGoalScrollPane) {
		this.awayTeamGoalScrollPane = awayTeamGoalScrollPane;
	}
	public JTable getHomeTeamGoalsTable() {
		return homeTeamGoalsTable;
	}
	public void setHomeTeamGoalsTable(JTable homeTeamGoalsTable) {
		this.homeTeamGoalsTable = homeTeamGoalsTable;
	}
	public JTable getAwayTeamGoalsTable() {
		return awayTeamGoalsTable;
	}
	public void setAwayTeamGoalsTable(JTable awayTeamGoalsTable) {
		this.awayTeamGoalsTable = awayTeamGoalsTable;
	}
	public JButton getBtnBack() {
		return btnBack;
	}
	public void setBtnBack(JButton btnBack) {
		this.btnBack = btnBack;
	}
	
}
